package com.wzh.glsc.cart.controller;

import com.wzh.common.utils.R;
import com.wzh.glsc.cart.service.CartService;
import com.wzh.glsc.cart.vo.CartItemVo;
import com.wzh.glsc.cart.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @author xiaoming
 * @PackageName:com.wzh.glsc.cart.controller
 * @ClassName:@{NAME}
 * @Description:
 * @date:@{DATE}@{TIME}
 */
@Controller
//@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @GetMapping({"/cartList", "/cartList.html", "cart.html"})
    public String cartList(Model model) throws ExecutionException, InterruptedException {
        CartVo cartVo = cartService.getCartList();
        model.addAttribute("cart",cartVo);
        return "cartList";
    }

    @GetMapping("/addCartItem")
    public String addCartItem(@RequestParam("skuId") Long skuId,
                             @RequestParam("num") Integer num,
                              RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {
        CartItemVo cartItemVo = cartService.addCart(skuId, num);
        redirectAttributes.addAttribute("skuId",skuId);
        return "redirect:http://cart.gulimall.com/addCartItemSuccess";
    }

    @GetMapping("/addCartItemSuccess")
    public String addCartItemSuccess(@RequestParam("skuId") Long skuId,
                                     Model model) throws ExecutionException, InterruptedException {
        CartItemVo cartItemVo = cartService.queryCartItemBySkuId(skuId);
        model.addAttribute("cartItem",cartItemVo);
        return "success";
    }


    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId,
                              @RequestParam("num") Integer num) throws ExecutionException, InterruptedException {
        cartService.countItem(skuId, num);
        return "redirect:http://cart.gulimall.com/cartList";
    }

    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId) throws ExecutionException, InterruptedException {
        cartService.deleteItem(skuId);
        return "redirect:http://cart.gulimall.com/cartList";
    }

    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId,
                             @RequestParam("checked") Integer checked)
            throws ExecutionException, InterruptedException {
        cartService.checkItem(skuId,checked);
        return "redirect:http://cart.gulimall.com/cartList";
    }


    @GetMapping("/getCheckedItem")
    @ResponseBody
    public R getCheckedItem() {
        List<CartItemVo> cartItems= cartService.checkedItem();
        return R.ok().setData(cartItems);
    }
}
